package com.banshuibao.bsb_android.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.util.IntentManager;

public class GuidePageActivity extends BaseActivity  {

    private ViewPager viewPager;
    private int imageId[] = {R.drawable.guide_one, R.drawable.guide_two, R.drawable.guide_three, R.drawable.guide_four, R.drawable.guide_five};
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_page);
        initView();
        editor = getSharedPreferences("guidePager", MODE_PRIVATE).edit();
    }

    public void initView() {
        viewPager = findViewById(R.id.viewPager);
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
        viewPager.setAdapter(myPagerAdapter);
    }

    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageId.length;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            FrameLayout frameLayout = new FrameLayout(GuidePageActivity.this);
            frameLayout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
            ImageView imageView = new ImageView(GuidePageActivity.this);
            imageView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
            imageView.setImageResource(imageId[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            ImageView icon = new ImageView(context);
            if(position == 0){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(0, 120, 60, 0);
                params.gravity = Gravity.END | Gravity.TOP;
                icon.setLayoutParams(params);
                icon.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_boot_page1));
            }else if(position == imageId.length-1){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(0, 120, 60, 0);
                params.gravity = Gravity.END | Gravity.TOP;
                icon.setLayoutParams(params);
                icon.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_boot_page5));
            }
            icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentManager.goUserHomeActivity(GuidePageActivity.this);
                    editor.putBoolean("isShow",true).commit();
                    finish();
                }
            });

            frameLayout.addView(imageView);
            frameLayout.addView(icon);
            container.addView(frameLayout);
            return frameLayout;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            //   super.destroyItem(container, position, object);
            container.removeView((View) object);
        }
    }
}
